﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Text;

namespace ZHDJ.Data.Models.SMS
{
    public class PartySmsmoRecord : EntityBase
    {
        #region 属性
        /// <summary>
        /// 联系人手机号
        /// </summary>
        [StringLength(20)]
        public string Mobile { get; set; }

        /// <summary>
        /// 短信内容
        /// </summary>
        [StringLength(1024)]
        public string Content { get; set; }

        [StringLength(50)]
        public string SendTime { get; set; }

        /// <summary>
        /// 留言时间（创建时间）
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [StringLength(512)]
        public string AddSerial { get; set; }

        /// <summary>
        /// 回复时间
        /// </summary>
        public DateTime? ReplyTime { get; set; }

        /// <summary>
        /// 回复内容
        /// </summary>
        [StringLength(1024)]
        public string ReplyContent { get; set; }

        /// <summary>
        /// 短信留言状态
        /// </summary>
        public int SmsState { get; set; }

        /// <summary>
        /// 回复人
        /// </summary>
        [StringLength(128)]
        public string HandleBy { get; set; }


        #endregion

        #region 查询管理
        public static DataSet GetListData(int pageIndex, int pageSize, string searchKey, int SmsState)
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "Begin",Value = (pageIndex)*pageSize},
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "PageSize",Value = pageSize}
            };

            StringBuilder sb = new StringBuilder();
            sb.Append(@"select SQL_CALC_FOUND_ROWS
 ID,Mobile,Content,CreateTime,AddSerial,ReplyTime,ReplyContent,SmsState,HandleBy
 from tblpartysmsmorecord where 1=1");

            if (SmsState == 1)
            {
                sb.Append(" and SmsState = 1");
            }
            if (SmsState == 4)
            {
                sb.Append(" and SmsState = 0");
            }
            if (!string.IsNullOrEmpty(searchKey))
            {
                sb.Append(" and Content like ?Content ");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "Content", Value = string.Format("%{0}%", searchKey) });
            }

            sb.Append(" order by CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }
        #endregion
    }
}
